home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / threadtest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-10  |  3.2 KB  |  151 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. /*
  22.   threadtest
  23.  
  24.   This program does nothing but generate a number of kernel threads
  25.   that allocate and free memory, with a variable
  26.   amount of "work" (i.e. cycle wasting) in between.
  27. */
  28.  
  29. #ifndef _REENTRANT
  30. #define _REENTRANT
  31. #endif
  32.  
  33. #include <iostream.h>
  34. #include <assert.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. #include "arch-specific.h"
  39. #include "timer.h"
  40.  
  41. int niterations = 50;    // Default number of iterations.
  42. int nobjects = 300000;  // Default number of objects.
  43. int nthreads = 1;    // Default number of threads.
  44. int work = 0;        // Default number of loop iterations.
  45. int size = 1;
  46.  
  47.  
  48. class Foo {
  49. public:
  50.   Foo (void)
  51.     : x (14),
  52.       y (29)
  53.     {}
  54.  
  55.   int x;
  56.   int y;
  57. };
  58.  
  59.  
  60. extern "C" void * worker (void *)
  61. {
  62.   int i, j;
  63.   Foo ** a;
  64.   a = new Foo * [nobjects / nthreads];
  65.  
  66.   for (j = 0; j < niterations; j++) {
  67.  
  68.     // printf ("%d\n", j);
  69.     for (i = 0; i < nobjects / nthreads; i++) {
  70.       a[i] = new Foo[size];
  71.       for (volatile int d = 0; d < work; d++) {
  72.     volatile int f = 1;
  73.     f = f + f;
  74.     f = f * f;
  75.     f = f + f;
  76.     f = f * f;
  77.       }
  78.       assert (a[i]);
  79.     }
  80.     
  81.     for (i = 0; i < nobjects / nthreads; i++) {
  82.       delete a[i];
  83.       for (volatile int d = 0; d < work; d++) {
  84.     volatile int f = 1;
  85.     f = f + f;
  86.     f = f * f;
  87.     f = f + f;
  88.     f = f * f;
  89.       }
  90.     }
  91.   }
  92.  
  93.   delete [] a;
  94.  
  95.   return NULL;
  96. }
  97.  
  98. #if defined(__sgi)
  99. #include <ulocks.h>
  100. #endif
  101.  
  102. int main (int argc, char * argv[])
  103. {
  104.   hoardThreadType * threads;
  105.   
  106.   if (argc >= 2) {
  107.     nthreads = atoi(argv[1]);
  108.   }
  109.  
  110.   if (argc >= 3) {
  111.     niterations = atoi(argv[2]);
  112.   }
  113.  
  114.   if (argc >= 4) {
  115.     nobjects = atoi(argv[3]);
  116.   }
  117.  
  118.   if (argc >= 5) {
  119.     work = atoi(argv[4]);
  120.   }
  121.  
  122.   if (argc >= 6) {
  123.     size = atoi(argv[5]);
  124.   }
  125.  
  126.   printf ("Running threadtest for %d threads, %d iterations, %d objects, %d work and %d size...\n", nthreads, niterations, nobjects, work, size);
  127.  
  128.   threads = new hoardThreadType[nthreads];
  129.   hoardSetConcurrency (nthreads);
  130.  
  131.   Timer t;
  132.  
  133.   t.start ();
  134.  
  135.   int i;
  136.   for (i = 0; i < nthreads; i++) {
  137.     hoardCreateThread (threads[i], worker, NULL);
  138.   }
  139.  
  140.   for (i = 0; i < nthreads; i++) {
  141.     hoardJoinThread (threads[i]);
  142.   }
  143.   t.stop ();
  144.  
  145.   printf( "Time elapsed = %f\n", (double) t);
  146.  
  147.   delete [] threads;
  148.  
  149.   return 0;
  150. }
  151.